<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.demo.grbkxt.model.Blog" %>
<%@ page import="com.example.demo.grbkxt.model.User" %>
<!DOCTYPE html>
<html>
<head>
    <title>管理员控制台</title>
    <%@ include file="/WEB-INF/views/common/header.jsp" %>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>管理员控制台</h1>
            <div class="nav-links">
                <a href="${pageContext.request.contextPath}/admin/users" class="btn">用户管理</a>
                <a href="${pageContext.request.contextPath}/admin/settings" class="btn">系统设置</a>
                <a href="${pageContext.request.contextPath}/logout" class="btn btn-danger">退出</a>
            </div>
        </div>
        
        <div class="stats">
            <h3>文章总数：${blogs.size()}</h3>
        </div>

        <div class="blog-form">
            <h2>发布新博客</h2>
            <form action="${pageContext.request.contextPath}/blog" method="post">
                <div>
                    <label>标题：</label><br/>
                    <input type="text" name="title" required style="width: 100%">
                </div>
                <div>
                    <label>内容：</label><br/>
                    <textarea name="content" required style="width: 100%; height: 200px;"></textarea>
                </div>
                <div>
                    <label>分类：</label><br/>
                    <select name="category" class="category-select">
                        <option value="">选择分类</option>
                        <option value="技术">技术</option>
                        <option value="生活">生活</option>
                        <option value="随笔">随笔</option>
                        <option value="其他">其他</option>
                    </select>
                </div>
                <div>
                    <label>
                        <input type="checkbox" name="adminOnly"> 仅管理员可见
                    </label>
                </div>
                <button type="submit" class="btn">发布</button>
            </form>
        </div>

        <div class="blog-list">
            <h2>所有博客</h2>
            <% 
            List<Blog> blogs = (List<Blog>)request.getAttribute("blogs");
            if(blogs != null) {
                for(Blog blog : blogs) { 
            %>
                <div class="blog-item">
                    <h3>
                        <a href="${pageContext.request.contextPath}/blog/view?id=<%= blog.getId() %>">
                            <%= blog.getTitle() %>
                        </a>
                    </h3>
                    <p><%= blog.getContent() %></p>
                    <div class="blog-meta">
                        <span>分类：<%= blog.getCategory() %></span>
                        <span>时间：<%= blog.getCreateTime() %></span>
                    </div>
                    <div class="blog-actions">
                        <a href="${pageContext.request.contextPath}/blog/edit?id=<%= blog.getId() %>" 
                           class="btn btn-primary">编辑</a>
                        <button onclick="deleteBlog('<%= blog.getId() %>')" 
                                class="btn btn-danger">删除</button>
                    </div>
                </div>
            <% 
                }
            } 
            %>
        </div>
    </div>
    <script>
        function editBlog(id) {
            window.location.href = '${pageContext.request.contextPath}/blog/edit?id=' + id;
        }
        
        function deleteBlog(id) {
            if(confirm('确定要删除这篇博客吗？')) {
                fetch('${pageContext.request.contextPath}/blog?id=' + id, {
                    method: 'DELETE'
                }).then(() => window.location.reload());
            }
        }
    </script>
</body>
</html>

</html>
    </div>
    <script>
        function editBlog(id) {
            window.location.href = '${pageContext.request.contextPath}/blog/edit?id=' + id;
        }
        
        function deleteBlog(id) {
            if(confirm('确定要删除这篇博客吗？')) {
                fetch('${pageContext.request.contextPath}/blog?id=' + id, {
                    method: 'DELETE'
                }).then(() => window.location.reload());
            }
        }
    </script>
</body>
</html>
